<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/26
 * Time: 15:01
 */

namespace app\api\controller;



use think\Db;

class Car extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 提交开通车辆网
     * @return \think\response\Json
     * vehicle_name     姓名
     * vehicle_code     车架号
     * vehicle_dealer_id  经销商
     * vehicle_img       行驶证主页
     * vehicle_img_deputy   行驶证副页
     * vehicle_phone     手机号
     * vehicle_images    申请车辆网图片集
     */
    public function openCarNew(){
        $vehicle_name = trim(input('vehicle_name'), ' ');
        $vehicle_code = trim(input('vehicle_code'), ' ');
        $vehicle_dealer_id = input('vehicle_dealer_id');
//        $vehicle_img = input('vehicle_img');
//        $vehicle_img_deputy = input('vehicle_img_deputy');
        $vehicle_phone = input('vehicle_phone');
        $vehicle_images = trim(input('vehicle_images'), ',');
        $already_apply = Db::table('up_vehicle')->where('vehicle_user_id', $this->user_id)->find();
        
        $data = [
            'vehicle_name' => $vehicle_name,
            'vehicle_code' => ',' . $vehicle_code,
            'vehicle_dealer_id' => $vehicle_dealer_id,
//            'vehicle_img' => $vehicle_img,
//            'vehicle_img_deputy' => $vehicle_img_deputy,
            'vehicle_phone' => $vehicle_phone,
            'vehicle_images' => $vehicle_images,
            'vehicle_time' => date('Y-m-d H:i:s')
        ];
        
        if($already_apply){
            $data['vehicle_status'] = 1;
            $data['vehicle_content'] = '';
            $res = Db::table('up_vehicle')->where('vehicle_user_id', $this->user_id)->update($data);
            if($res){
                return json(['status' => 200, 'msg' => '更新成功']);
            }else{
                return json(['status' => 0, 'msg' => '更新信息失败']);
            }
        }else{
            $data['vehicle_user_id'] = $this->user_id;
            $res = Db::table('up_vehicle')->insertGetId($data);
            if($res){
                // 消息
                $title = '您已成功申请开通车辆网，工作人员会尽快处理，请您耐心等待';
                $notice = [
                    'notice_type' => 2,
                    'notice_cate' => 4,
                    'notice_subject_id' => $res,
                    'notice_title' => $title,
                    'notice_user' => ',0,' . $this->user_id . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time,
                ];
                Db::table('up_notice')->insert($notice);
                try{
                    $send_user = (string)$this->user_id;
                    $this->Jpush(1, $send_user, $title, ['send_type' => 8, 'vehicle_id' => $res]);
                }catch(\Exception $e){
            
                }
                return json(['status' => 200, 'msg' => '提交成功']);
            }else{
                return json(['status' => 0, 'msg' => '提交失败']);
            }
        }
    }
    
    /**
     * 车辆网申请信息回显
     * @return \think\response\Json
     */
    public function clwShowNew(){
        $apply_info = Db::table('up_vehicle')
            ->join('up_dealer', 'dealer_id = vehicle_dealer_id', 'left')
            ->field('vehicle_name, vehicle_code, vehicle_dealer_id, vehicle_img, vehicle_img_deputy,
            vehicle_status, vehicle_phone, dealer_name, vehicle_images, vehicle_id')
            ->where('vehicle_user_id', $this->user_id)
            ->find();
        if($apply_info){
            // 车架号
            $apply_info['vehicle_code'] = ltrim($apply_info['vehicle_code'], ',');
            // 图集
            if($apply_info['vehicle_images']){
                $images = explode(',', $apply_info['vehicle_images']);
            }else{
                $images = [];
            }
            $apply_info['vehicle_images'] = $images;
        }else{
            $apply_info = [];
        }
        return json(['status' => 200, 'msg' => $apply_info]);
    }
    
    /**
     * 提交开通车辆网
     * @return \think\response\Json
     * vehicle_images    申请车辆网图片集
     */
    public function openCar(){
        $params = input('');
        $already_apply = Db::table('up_vehicle')->where('vehicle_user_id', $this->user_id)->find();
        $data = [
            'vehicle_time' => date('Y-m-d H:i:s'),
            'vehicle_images' => trim($params['vehicle_images'], ',')
        ];
        if($already_apply){
            $data['vehicle_status'] = 1;
            $res = Db::table('up_vehicle')->where('vehicle_user_id', $this->user_id)->update($data);
            if($res){
                return json(['status' => 200, 'msg' => '更新成功']);
            }else{
                return json(['status' => 0, 'msg' => '更新信息失败']);
            }
        }else{
            $data['vehicle_user_id'] = $this->user_id;
            $res = Db::table('up_vehicle')->insertGetId($data);
            if($res){
                // 消息
                $title = '您已成功申请开通车辆网，工作人员会尽快处理，请您耐心等待';
                $notice = [
                    'notice_type' => 2,
                    'notice_cate' => 4,
                    'notice_subject_id' => $res,
                    'notice_title' => $title,
                    'notice_user' => ',0,' . $this->user_id . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time,
                ];
                Db::table('up_notice')->insert($notice);
                try{
                    $send_user = (string)$this->user_id;
                    $this->Jpush(1, $send_user, $title, ['send_type' => 8, 'vehicle_id' => $res]);
                }catch(\Exception $e){
                    
                }
                return json(['status' => 200, 'msg' => '提交成功']);
            }else{
                return json(['status' => 0, 'msg' => '提交失败']);
            }
        }
    }
    
    /**
     * 车辆网申请信息回显
     * @return \think\response\Json
     */
    public function clwShow(){
        $apply_info = Db::table('up_vehicle')
            ->field('vehicle_images')
            ->where('vehicle_user_id', $this->user_id)
            ->find();
        if($apply_info){
            if($apply_info['vehicle_images']){
                $images = explode(',', $apply_info['vehicle_images']);
            }else{
                $images = [];
            }
            $apply_info['vehicle_images'] = $images;
        }else{
            $apply_info = [];
        }
        return json(['status' => 200, 'msg' => $apply_info]);
    }
}